package com.eye.catcher.service.generate.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eye.catcher.utils.RandomIdUtils;
import com.eye.catcher.constants.SbCode;
import com.eye.catcher.entity.TSysDaRole;
import com.eye.catcher.mapper.TSysDaRoleMapper;
import com.eye.catcher.service.generate.ITSysDaRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eye.catcher.system.role.param.RoleDto;
import com.eye.catcher.system.role.param.RolePage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huzhiwei
 * @since 2022-02-08
 */
@Slf4j
@Service
public class TSysDaRoleServiceImpl extends ServiceImpl<TSysDaRoleMapper, TSysDaRole> implements ITSysDaRoleService {

    @Autowired
    private TSysDaRoleMapper roleMapper;

    @Override
    public List<RoleDto> getRoleByUserId(String userId) {
        return roleMapper.getRoleByUserId(userId);
    }

    @Override
    public void addRole(TSysDaRole role) {
        role.setId(RandomIdUtils.getGuid());
        role.setLastTime(LocalDateTime.now());
        role.setSource(SbCode.DATA_SOURCE_CREATE.getCode());
        this.save(role);
    }
    @Override
    public void editRole(TSysDaRole role) {
        QueryWrapper<TSysDaRole> wrapper = Wrappers.query();
        wrapper.eq("ID",role.getId());
        this.update(role,wrapper);
    }
    @Override
    public void deleteRoleByIds(String roleIds) {
        this.removeByIds(Arrays.asList(roleIds.split(",")));
    }

    @Override
    public Object getPageRoleList(RolePage rolePage) {
        Page<TSysDaRole> page=new Page<>(rolePage.getPage().getPageNum(),rolePage.getPage().getPageSize());
        QueryWrapper<TSysDaRole> wrapper = Wrappers.query();
        //组装查询条件
        if(StringUtils.isNotEmpty(rolePage.getRole().getCode())){
            wrapper.eq("CODE",rolePage.getRole().getCode());
        }
        if(StringUtils.isNotEmpty(rolePage.getRole().getName())){
            wrapper.like("NAME",rolePage.getRole().getName());
        }
        wrapper.eq("STATUS", SbCode.DATA_NORMAL.getCode());
//        wrapper.orderByAsc(" SORT ");
        //list
        IPage<TSysDaRole> returnPage = this.page(page, wrapper);
        //总数
        returnPage.setTotal(this.count(wrapper));
        return returnPage;
    }

    @Override
    public void updateStausByIds(String ids) {
        QueryWrapper<TSysDaRole> wrapper = Wrappers.query();
        wrapper.in("ID",Arrays.asList(ids.split(",")));
        List<TSysDaRole> list = this.list(wrapper);
        List<TSysDaRole> updateBatchList = new ArrayList<>();
        for(TSysDaRole role : list){
            if(SbCode.DATA_NORMAL.getCode().equals(role.getStatus())){
                role.setStatus(SbCode.DATA_DISABLE.getCode());
            }else{
                role.setStatus(SbCode.DATA_NORMAL.getCode());
            }
            updateBatchList.add(role);
        }
        this.updateBatchById(updateBatchList);
    }
}
